package com.train.trainmanage.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.train.trainmanage.mapper.ComeGoDirectionModelMapper;
import com.train.trainmanage.mapper.DailyPlanTrainModelMapper;
import com.train.trainmanage.mapper.StationModelMapper;
import com.train.trainmanage.mapper.TrainModelMapper;
import com.train.trainmanage.model.ComeGoDirectionModel;
import com.train.trainmanage.model.DailyPlanTrainModel;
import com.train.trainmanage.model.DepartmentModel;
import com.train.trainmanage.model.LocomotiveModel;
import com.train.trainmanage.model.StationModel;
import com.train.trainmanage.model.TrainModel;
import com.train.trainmanage.service.DailyPlanTrainService;

@Service
public class DailyPlanTrainServiceImpl implements DailyPlanTrainService{

	@Autowired
	private DailyPlanTrainModelMapper dailyPlanMapper;
	@Autowired
	private TrainModelMapper trainMapper;
	@Autowired
	private StationModelMapper stationMapper;
	@Autowired
	private ComeGoDirectionModelMapper comeGoDirecMapper;
	
	@Override
	public int delOldDailyPlan() {
		return dailyPlanMapper.deleteAll();
	}

	@Override
	public int saveByExcle(List<DailyPlanTrainModel> record) {
		int result = 0;
		if(record != null && record.size()>0) {
			for(DailyPlanTrainModel temPlan : record) {
				String trainName = temPlan.getTrainName();
				TrainModel train = trainMapper.selectByTrainName(trainName);
				if(train != null) {
					temPlan.setStartStationId(train.getStartStationId());
					temPlan.setEndStationId(train.getEndStationId());
					temPlan.setAfterStationId(train.getAfterStationId());
					temPlan.setFrontStationId(train.getFrontStationId());
					temPlan.setReachTime(train.getReachTime());
					temPlan.setLeaveTime(train.getLeaveTime());
					temPlan.setPickUpTrack(train.getPickUpTrack());
					temPlan.setDepartureTrack(train.getDepartureTrack());
					temPlan.setPickUpSequence(train.getPickUpSequence());
					temPlan.setDepartureSequence(train.getDepartureSequence());
					temPlan.setComeDirectionId(train.getComeDirectionId());
					temPlan.setGoDirectionId(train.getGoDirectionId());
					temPlan.setIsAddWater(train.getIsAddWater());
					temPlan.setIsDealRubbish(train.getIsDealRubbish());
					result = dailyPlanMapper.insertSelective(temPlan); 
				}
			}
		}
		
		return result;
	}

	@Override
	public List<DailyPlanTrainModel> listByTrainModel(DailyPlanTrainModel record) {
		Map<String,Object> map = new HashMap<String,Object>(); 
		if(record != null) {
			if(record.getTrainName() != null) {
				map.put("trainName", record.getTrainName());
			}
			if(record.getStartStationId() != null) {
				map.put("startStationId", record.getStartStationId());
			}
			if(record.getEndStationId() != null) {
				map.put("endStationId", record.getEndStationId());
			}
			if(record.getIsAddWater() != null) {
				map.put("isAddWater", record.getIsAddWater());
			}
			if(record.getIsDealRubbish() != null) {
				map.put("isDealRubbish", record.getIsDealRubbish());
			}
			if(record.getReachTime() != null) {
				map.put("reachTime", record.getReachTime());
			}
			if(record.getLeaveTime() != null) {
				map.put("leaveTime", record.getLeaveTime());
			}
			
		}
		
		List<DailyPlanTrainModel> result = dailyPlanMapper.listByMap(map);
		return result;
	}

	@Override
	public DailyPlanTrainModel getInfoById(Integer id) {
		DailyPlanTrainModel train = dailyPlanMapper.selectByPrimaryKey(id);
		if(train != null) {
			StationModel startStation = stationMapper.selectByPrimaryKey(train.getStartStationId());
			if(startStation != null) {
				train.setStartStationName(startStation.getStationName());
			}
			
			StationModel endStation = stationMapper.selectByPrimaryKey(train.getEndStationId());
			if(endStation != null) {
				train.setEndStationName(endStation.getStationName());
			}
			
			ComeGoDirectionModel comeDirec = comeGoDirecMapper.selectByPrimaryKey(train.getComeDirectionId());
			if(comeDirec != null) {
				train.setComeDirectionName(comeDirec.getDirecName());
			}
			ComeGoDirectionModel goDirec = comeGoDirecMapper.selectByPrimaryKey(train.getGoDirectionId());
			if(goDirec != null) {
				train.setGoDirectionName(goDirec.getDirecName());
			}
		}
		
		return train;
	}

	@Override
	public int saveDailyPlanTrain(DailyPlanTrainModel record) {
		int result = 0;
		Integer id = record.getId();
		if(id != null && id > 0) {
			result = dailyPlanMapper.updateByPrimaryKeySelective(record);
		}else {
			result = dailyPlanMapper.insertSelective(record);
		}
		return result;
	}
	
}
